package cn.codecrab.ai.langchain4j.config.provider;

import cn.codecrab.ai.langchain4j.store.MongoChatMemoryStore;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 王刚
 * @since 2025年05月10日 13:27
 */
@Configuration
public class ChatMemoryProviderConfig {

    /** 内存中的全部聊天记录都在这个对象的Map中，不要直接在匿名函数里new，这样的话每一个不同的memoryId对应的都是不用的MemoryStore */
    private static final InMemoryChatMemoryStore inMemoryChatMemoryStore = new InMemoryChatMemoryStore();

    @Bean
    public ChatMemoryProvider chatMemoryProvider() {
        return memoryId -> MessageWindowChatMemory.builder().id(memoryId).maxMessages(10).build();
    }

    @Bean
    public ChatMemoryProvider chatMemoryProvider2() {
        return memoryId -> MessageWindowChatMemory.builder().id(memoryId).chatMemoryStore(inMemoryChatMemoryStore).maxMessages(10).build();
    }

    @Bean
    public ChatMemoryProvider mongoChatMemoryProvider(MongoChatMemoryStore mongoChatMemoryStore) {
        return memoryId -> MessageWindowChatMemory.builder().id(memoryId).chatMemoryStore(mongoChatMemoryStore).maxMessages(20).build();
    }

}
